﻿using HrEFCore;
using HrEFCore.Models;
using HrService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using Microsoft.Win32;
using Newtonsoft.Json;
using System.Net.NetworkInformation;
using System.Runtime.Intrinsics.X86;

namespace HrUI.Controllers
{
	public class SGController : Controller
	{
		private readonly MyDbContext myDbContext;
		private readonly SGService service;
		private readonly SGDService sgdService;
		private readonly SSDService ssdService;

		public SGController(MyDbContext myDbContext, SGService service, SGDService sgdService, SSDService ssdService)
		{
			this.myDbContext = myDbContext;
			this.service = service;
			this.sgdService = sgdService;
			this.ssdService = ssdService;
		}
		public async Task<ActionResult> Query()
		{

			return View();
		}

		public ActionResult Query_List()
		{
			return View();
		}
		public ActionResult GetQuery(string selectedValue)
		{
			List<HF> hf = service.HFSelectAsync(selectedValue);
			return Json(hf);
		}
		public async Task<ActionResult> Insert()
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;
			return View();
		}
		//public async Task<ActionResult> GetSGInsert(SG sg)
		//{
		//	int row = await service.SGInsert(sg);
		//	return Json(row);
		//}
		public async Task<ActionResult> GetSGDInsert(string tableData)
		{
			var jsonTableDate = JsonConvert.DeserializeObject<List<HFSum>>(tableData);
			List<SGD> sgd = new List<SGD>();
			var SalaryGrantId = HttpContext.Session.GetString("SalaryGrantId");
			if (jsonTableDate != null)
			{
				foreach (var item in jsonTableDate)
				{
					SGD sg = new SGD()
					{
						SalaryGrantId = SalaryGrantId,
						HumanId = item.HumanId,
						HumanName = item.HumanName,
						BounsSum = item.BounsSum,
						SaleSum = item.SaleSum,
						DeductSum = item.DeductSum,
						SalaryStandardSum = item.SalarySum,
						SalaryPaidSum = item.SalaryPaidSum,
					};
					//HttpContext.Session.SetString("SalaryGrantId", SalaryGrantId);
					sgd.Add(sg);
				}
			}
			int row = await sgdService.SGDInsert(sgd);
			return Json(row);
		}
		public async Task<ActionResult> GetInsert(SG sg)
		{
			string Register = HttpContext.Session.GetString("UTrueName");
			SG sg2 = new SG()
			{
				SalaryGrantId = sg.SalaryGrantId,
				SalaryStandardId = sg.SalaryStandardId,
				FirstKindId = sg.FirstKindId,
				FirstKindName = sg.FirstKindName,
				SecondKindId = sg.SecondKindId,
				SecondKindName = sg.SecondKindName,
				ThirdKindId = sg.ThirdKindId,
				ThirdKindName = sg.ThirdKindName,
				HumanAmount = sg.HumanAmount,
				SalaryStandardSum = sg.SalaryStandardSum,
				SalaryPaidSum = sg.SalaryPaidSum,
				Register = Register,
				RegistTime = sg.RegistTime,
				Checker = "",
				CheckTime = DateTime.Now,
				CheckStatus = 0
			};
			HttpContext.Session.SetString("SalaryGrantId", sg.SalaryGrantId);
			int row = await service.SGInsert(sg2);
			return Json(row);
		}


		//根据薪酬标准编号查
		public async Task<ActionResult> HFSGById(string SalaryStandardId)
		{
			HF hf = await service.HFSGById(SalaryStandardId);
			return Json(hf);
		}
		public ActionResult GetHF(string SalaryStandardId, string tiao)
		{
			List<HF> hf = service.GetHF(SalaryStandardId, tiao);
			return Json(hf);
		}
		public ActionResult JS(string SalaryStandardId)
		{
			int hf = service.Amount(SalaryStandardId);
			return Json(hf);
		}

		//复核查看
		public IActionResult Index()
		{
			return View();
		}
		public async Task<ActionResult> GetIndex(FenYe<SG> fenYe)
		{
			fenYe = await service.SGFenYeSelect(fenYe);
			return Json(fenYe);
		}
		//复核填充
		public async Task<ActionResult> FHUpdate(string id)
		{
			string uname = HttpContext.Session.GetString("UTrueName");
			ViewBag.uname = uname;

			SG sg = await service.SGById(id);
			ViewData.Model = sg;



			HttpContext.Session.SetString("id", sg.SalaryStandardId);
			HttpContext.Session.SetString("id2", sg.SalaryGrantId);
			return View();
		}
		public ActionResult getSGDById(string SalaryGrantId)
		{
			//string id = HttpContext.Session.GetString("id2");
			List<SGD> sgd = sgdService.SGDById(SalaryGrantId);
			return Json(sgd);
		}
		public async Task<ActionResult> Update(SG sg, SGD sgd)
		{
			sg.CheckStatus = 1;
			int row = await service.SGFHUpdate(sg);
			int result = await sgdService.SGDFHUpdate(sgd);
			if (row > 0 && result > 0)
			{
				Content("<script>alert('复核成功')</script>");
				return RedirectToAction("Index", "SG");
			}
			else
			{
				Content("<script>alert('复核失败')</script>");
				return View();
			}
		}
		//查看ssd详细内容
		public ActionResult SSDSelect(string StandardId)
		{

			List<SSD> ssd = service.SSDById(StandardId);
			return Json(ssd);
		}
		//薪酬发放模糊查询
		public async Task<IActionResult> SGWhere()
		{
			return View();
		}
		//薪酬发放下拉
		public async Task<IActionResult> SGWhereXL()
		{
			List<SG> list = await service.SGSelectXL();
			return Json(list);
		}
		//薪酬发放模糊查询列表
		public async Task<ActionResult> SGWhereCX(SG sg, DateTime stateDate, DateTime endDate)
		{
			string where = " and SalaryGrantId like '%%' ";
			if (sg.SalaryGrantId != null)
			{
				where = " and SalaryGrantId like '%" + sg.SalaryGrantId + "%' ";
			}
			if (sg.Register != null)
			{
				where = " and Register like '%" + sg.Register + "%' ";
			}
			if (stateDate != DateTime.MinValue && endDate != DateTime.MinValue)
			{
				where = " and RegistTime  between '" + stateDate + "' and '" + endDate + "' ";
			}
			if (stateDate != DateTime.MinValue && endDate == DateTime.MinValue)
			{
				where = " and RegistTime  > '" + stateDate + "' ";
			}
			if (stateDate == DateTime.MinValue && endDate != DateTime.MinValue)
			{
				where = " and RegistTime  < '" + endDate + "' ";
			}

			if (where == null)
			{
				where = " and SalaryGrantId like '%%' ";
			}
			//HttpContext.Session.SetString("StandardId",ss.StandardId);
			HttpContext.Session.SetString("where3", where);
			return View();
		}

		public async Task<IActionResult> GetSGWhereCX(int currentPage, int pageSize)
		{
			string where = HttpContext.Session.GetString("where3");
			FenYe<SG> fenYe = await service.SGWhere(currentPage, pageSize, where);
			return Json(fenYe);
		}
	}
}
